﻿using UnityEngine;
using System.Collections;
using Game.Scripts.Managers;
using Assets.components;

public class DragBuildingController : MonoBehaviour {

    public static DragBuildingController CreateComponent(GameObject gameObject)
    {
        DragBuildingController dbc = gameObject.AddComponent<DragBuildingController>();
        return dbc;
    }

    private FrenzyIslandGenerator island;
    private VoxelWorld voxelWorld;
    private Building building;

    public bool IsBuildingAllowed
    {
        get {
            return isBuildingAllowed;
        }
        set
        {
            isBuildingAllowed = value;
        }
    }
    private bool isBuildingAllowed = false;

    // Use this for initialization
    void Start () {
        voxelWorld = BuildingManager.instance.voxelWorld;
        island = voxelWorld.GetComponent<FrenzyIslandGenerator>();
        building = GetComponent<Building>();

        Vector3 pos = transform.position;
        int x = (int)pos.x;
        int y = (int)pos.y;
        int z = (int)pos.z;
        isBuildingAllowed = building.prototype.isBuildingAllowedAt(x, y, z);
    }
	
	// Update is called once per frame
	void Update () {
	
	}

    void OnMouseDrag()
    {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out hit, BuildingManager.instance.layerToBeIgnoreBuildingPreview)){
            Vector3 voxelHitPosition = this.getPositionFromHit(hit, false);
            voxelHitPosition = new Vector3( 
                (int)voxelHitPosition.x, 
                (int)voxelHitPosition.y, 
                (int)voxelHitPosition.z);

            int x = (int)voxelHitPosition.x;
            int y = (int)voxelHitPosition.y;
            int z = (int)voxelHitPosition.z;

            //fix bug costruzione salterella
            if(voxelWorld.hasVoxelAt(x,y,z)){
                Voxel voxel = voxelWorld.getVoxelAt(x,y,z);
                if(!voxel.prototype.solid){
                    IsBuildingAllowed = false;
                    transform.position = new Vector3(x, y, z);
                }else{
                    isBuildingAllowed = building.prototype.isBuildingAllowedAt(x, y, z);
                    voxelWorld.SetHeightOverMarchingCube(voxelHitPosition, this.gameObject);
                }
            }
        }

    }

    private float moveWithinVoxel(float pos, float norm, bool adjacent)
    {
        if (pos - (int)pos == 0.5f || pos - (int)pos == -0.5f)
        {
            if (adjacent)
            {
                pos += (norm / 2);
            }
            else
            {
                pos -= (norm / 2);
            }
        }

        return (float)pos;
    }

    private Vector3 getPositionFromHit(RaycastHit hit, bool adjacent)
    {
        float x = this.moveWithinVoxel(hit.point.x, hit.normal.x, adjacent);
        float y = this.moveWithinVoxel(hit.point.y, hit.normal.y, adjacent);
        float z = this.moveWithinVoxel(hit.point.z, hit.normal.z, adjacent);

        return new Vector3(Mathf.Round(x), Mathf.Round(y), Mathf.Round(z));
    }
}
